package com.cn.wanxi.controller;

import com.cn.wanxi.common.ParseTool;
import com.cn.wanxi.entity.ResultEntity;
import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/back/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/add")
    protected ResultEntity add(UserEntity userEntity){
        userEntity.setPassword(ParseTool.MD5(userEntity.getPassword()));
        return userService.add(userEntity);
    }

    @RequestMapping("/del")
    protected ResultEntity delete(UserEntity userEntity){
        return userService.delete(userEntity);
    }

    @RequestMapping("/get")
    protected ResultEntity findAll(UserEntity userEntity){
        return ResultEntity.success(userService.findAll(userEntity));
    }

    @RequestMapping("/id")
    protected ResultEntity findById(UserEntity userEntity){
        return userService.findById(userEntity);
    }

    @RequestMapping("/post")
    protected ResultEntity update(UserEntity userEntity){
        if (userEntity.getPassword() != null) {
            userEntity.setPassword(ParseTool.MD5(userEntity.getPassword()));
        }
        return userService.update(userEntity);
    }
}
